package com.sto.stoapp.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.log.ExceptionLog;

import sto.android.app.StoPdaKeyEvent;

/**
 * 自定义dialog
 * 
 * @author mgf
 * 
 */
public class BaseDialog extends Dialog {

	private String title;

	private String message;

	private String presentationMessage;

	private Context context;

	private View view;

	private TextView vTitle;

	private TextView vPresentation;

	private TextView vMessage;

	private Button vAccept;

	private Button vCanncel;

	private boolean enabledKeyEvent;

	private boolean isOpenKeyListener = false;

	// 正确、取消
	private String confirm, cancel;

	private OnBackClickListener onBackClickListener;

	/**
	 * BaseDialog
	 * 
	 * @param context
	 *            tag
	 */
	public BaseDialog(Context context) {
		super(context, R.style.dailog_base_style);
		// setTitle(R.string.logout_confirm_msg);
		this.context = context;
		setEnabledKeyEvent(true);
		setCancelable(false);
		setCanceledOnTouchOutside(false);
	}

	/**
	 * setLayout
	 * 
	 * @param id
	 *            tag
	 */
	public void setLayout(int id) {
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(id, null);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		keyCode=StoApplication.getStoJni().getEventFuction(keyCode);
		if (enabledKeyEvent) {
			switch (keyCode) {
			// �ҹ��ܼ�
			case StoPdaKeyEvent.KEYCODE_SOFT_LEFT:
				if (onBackClickListener != null) {
					onBackClickListener.onBackClickListener(true);
				}
				dismiss();
				break;
			// ���ܼ�
			case StoPdaKeyEvent.KEYCODE_DEL:
				if (onBackClickListener != null) {
					onBackClickListener.onBackClickListener(false);
				}
				dismiss();
				break;
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	// ����home��
	@Override
	public void onAttachedToWindow() {
		// this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
		super.onAttachedToWindow();
	}

	/**
	 * show
	 */
	@Override
	public void show() {
		try {
			if (view == null) {
				setDefalutLayout();
			}
			setContentView(view);
			Window window = this.getWindow();
			WindowManager.LayoutParams windowWM = window.getAttributes();
			window.setAttributes(windowWM);
			window.setLayout(
					(int) context.getResources().getDimension(
							R.dimen.dialog_width), LayoutParams.WRAP_CONTENT);
			window.setGravity(Gravity.CENTER);
			findView();
			super.show();
		} catch (Exception e) {
			ExceptionLog.writeLog("BaseDialog show", e.getMessage());
		}

	}

	/**
	 * showLog
	 * 
	 * @param v
	 *            tag
	 */
	public void showLog(View v) {
		setContentView(v);
		Window window = this.getWindow();
		WindowManager.LayoutParams windowWM = window.getAttributes();
		// windowWM.x = 30;
		// windowWM.y = 30;
		window.setAttributes(windowWM);
		window.setLayout(
				(int) context.getResources().getDimension(R.dimen.dialog_width),
				LayoutParams.WRAP_CONTENT);
		window.setGravity(Gravity.CENTER);
		super.show();
	}

	private void setDefalutLayout() {
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.user_defined_alert, null);

	}

	/**
	 * set dialog layout before show
	 * 
	 * @author wxf
	 * 
	 */
	public void setDefalutLayout1() {
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.user_defined_alert1, null);

	}

	/**
	 * setAnimation
	 * 
	 * @param animation
	 *            tag
	 */
	public void setAnimation(Animation animation) {
		view.setAnimation(animation);
	}

	private void findView() {
		vTitle = (TextView) view.findViewById(R.id.version_info);
		vPresentation = (TextView) view.findViewById(R.id.prompt);
		vMessage = (TextView) view.findViewById(R.id.time_info);
		vMessage.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		vAccept = (Button) view.findViewById(R.id.yes);
		vCanncel = (Button) view.findViewById(R.id.no);

		if (confirm != null && !confirm.equals("")) {
			vAccept.setText(confirm);
			vAccept.requestFocus();
		}

		if (cancel != null && !cancel.equals("")) {
			vCanncel.setText(cancel);
		}
		setText();
		vAccept.setOnClickListener(new StoOnClickListener((Activity) context) {

			@Override
			public void mHonClick(View view) {
				dismiss();
				onBackClickListener.onBackClickListener(true);
			}

		});

		vCanncel.setOnClickListener(new StoOnClickListener((Activity) context) {

			@Override
			public void mHonClick(View view) {
				dismiss();
				onBackClickListener.onBackClickListener(false);
			}
		});
	}

	private void setText() {
		vTitle.setText(title);
		if (presentationMessage == null
				|| presentationMessage.trim().equals("")) {
			vPresentation.setVisibility(View.INVISIBLE);
		} else {
			vPresentation.setVisibility(View.VISIBLE);
			vPresentation.setText(presentationMessage);
		}

		if (message == null || message.trim().equals("")) {
			vMessage.setVisibility(View.INVISIBLE);
		} else {
			vMessage.setVisibility(View.VISIBLE);
			vMessage.setText(message);
		}

	}

	public TextView getvTitle() {
		return vTitle;
	}

	public TextView getvPresentation() {
		return vPresentation;
	}

	public TextView getvMessage() {
		return vMessage;
	}

	/**
	 * setTitles
	 * 
	 * @param titleTag
	 *            tag
	 */
	public void setTitles(String titleTag) {
		this.title = titleTag;
	}

	public void setMessage(String msg) {
		this.message = msg;
	}

	public void setPresentationMessage(String presentationMessage) {
		this.presentationMessage = presentationMessage;
	}

	public void setOnBackClickListener(OnBackClickListener onBackClickListener) {
		this.onBackClickListener = onBackClickListener;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public void setCancel(String cancel) {
		this.cancel = cancel;
	}

	public Button getvAccept() {
		return vAccept;
	}

	public void setvAccept(Button vAccept) {
		this.vAccept = vAccept;
	}

	public Button getvCanncel() {
		return vCanncel;
	}

	public void setvCanncel(Button vCanncel) {
		this.vCanncel = vCanncel;
	}

	public void setEnabledKeyEvent(boolean enabledKeyEvent) {
		this.enabledKeyEvent = enabledKeyEvent;
	}

	/**
	 * dismiss
	 */
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();

	}

	/**
	 * 扫描键触发dismiss效果
	 *  cjb
	 * @param openScanListener 是否打开  默认不打开
	 */
	public void openScanKeyListener(boolean openScanListener) {
		isOpenKeyListener = openScanListener;
	}

	/**
	 * 监听按键事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		keyCode= StoApplication.getStoJni().getEventFuction(keyCode);
		if (isOpenKeyListener) {
			try {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					switch (keyCode) {
					case StoPdaKeyEvent.KEYCODE_SCAN:
						this.dismiss();
						break;

					default:
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("onKeyDown", e.getMessage());
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
